/** 一个盒子分为四个象限 当被移动盒子的四个顶点位于对应象限内 满足交换条件*/
function pointIn(a, b, bCenter) {
  return ((a.x > b.x && a.x < bCenter.x) || (a.x > bCenter.x && a.x < b.x)) && ((a.y > b.y && a.y < bCenter.y) || (a.y > bCenter.y && a.y < b.y))
}

export function collision(a, aDom, b, bDom) {
  if (a.key === b.key) return false;
  let aLeft = aDom.offsetLeft;
  let aTop = aDom.offsetTop;
  let aWidth = aDom.clientWidth;
  let aHeight = aDom.clientHeight;
  /*
          -------------------------> x
          |     |
          |     | top
          |-----------------
          | left|  weight   | height
          |     |           |
          |     ------------
          |
          ↓ y

   */
  let aTopLeft = {x: aLeft, y: aTop};
  let aTopRight = {x: aLeft + aWidth, y: aTop};
  let aBottomLeft = {x: aLeft, y: aTop + aHeight};
  let aBottomRight = {x: aLeft + aWidth, y: aTop + aHeight};
  let bTopLeft = {x: bDom.offsetLeft, y: bDom.offsetTop};
  let bTopRight = {x: bDom.offsetLeft + bDom.w, y: bDom.offsetTop};
  let bBottomLeft = {x: bDom.offsetLeft, y: bDom.offsetTop + bDom.h};
  let bBottomRight = {x: bDom.offsetLeft + bDom.w, y: bDom.offsetTop + bDom.h};
  //let aCenter={x:(aTopLeft.x+aBottomRight.x)/2,y:(aTopLeft.y+aBottomRight.y)/2};
  let bCenter = {x: (bTopLeft.x + bBottomRight.x) / 2, y: (bTopLeft.y + bBottomRight.y) / 2};
  return !!(pointIn(aTopLeft, bTopLeft, bCenter) || pointIn(aTopRight, bTopRight, bCenter) || pointIn(aBottomLeft, bBottomLeft, bCenter) || pointIn(aBottomRight, bBottomRight, bCenter));
}

/** 获取第一个有碰撞的盒子*/
export function getFirstCollison(lay, layoutItem, itemDom, col) {
  let layout = [].concat(initLayout(lay, col)).filter(item => item.show).sort((a, b) => a.key - b.key);
  let children = document.getElementsByClassName("layout-item");
  let childContain = [];
  let childMap = new Map();

  /** 处理有模块隐藏的情况 保证children[index]存在*/
  let layoutTemp = layout.map(item => {
    return JSON.parse(JSON.stringify(item))
  });
  layoutTemp.map((item, index) => childMap.set(item.key, children[index]));
  layoutTemp.sort((a, b) => a.order - b.order).map(item => childContain.push(childMap.get(item.key)));

  for (let i = 0; i < layoutTemp.length; i++) {
    if (layoutTemp[i].show) {
      let cLayout = collision(layoutTemp[i], childContain[i], layoutItem, itemDom);
      if (childContain[i] && cLayout ) {
        return layoutTemp[i]
      }
    }
  }
  return null
}

/**初始化布局，由w,h 计算GridX,GridY*/
export function initLayout(layout, col) {
  let GridX = 0;
  let GridY = 0;
  let lastRowHeight = 0;
  let hideArr = [];
  layout.map(item => {if(!item.show) hideArr.push(item)});
  let layoutTemp = layout.sort((a, b) => a.order - b.order).filter(item => item.show);
  layoutTemp.map((item, index) => {
    if (!item.show) return true;
    if (GridX + item.w > col || (index && (GridX + layoutTemp[index - 1].w === col))) {
      GridX = 0;
      lastRowHeight += layoutTemp[index - 1].h;
      item.GridX = 0;
      item.GridY = lastRowHeight;
    } else {
      index && (GridX += layoutTemp[index - 1].w);
      item.GridX = GridX;
      item.GridY = lastRowHeight;
    }
  });
  layoutTemp = [...layoutTemp, ...hideArr];
  layoutTemp.sort((a, b) => a.key - b.key);
  return layoutTemp
}

export function isEqual(a, b) {
  let aKeys = Object.keys(a);
  let bKeys = Object.keys(b);
  if (aKeys.sort().toString() !== bKeys.sort().toString()) {
    return false
  } else {
    let re = true;
    aKeys.map(item => {
      a[item] !== b[item] && (re = false)
    });
    return re
  }
}
